package cn.rick.controller;

import cn.dev33.satoken.util.SaResult;
import cn.rick.entity.SysDictData;
import cn.rick.service.ISysDictDataService;
import cn.rick.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author Rick
 * @date 2022-10-21 10:59
 */
@RestController
@RequestMapping("/api/dictData")
@Api(value = "字典数据控制", tags = {"字典数据控制"})
public class DictDataController {

    @Resource
    ISysDictDataService iSysDictDataService;

    @GetMapping("/getDataByParams")
    @ApiOperation("通过参数查询所有字典数据")
    public SaResult getDataByParams(SysDictData dictData) {
        return SaResult.data(iSysDictDataService.getDataByParams(dictData));
    }

    @GetMapping("/getDictDataByPage")
    @ApiOperation("分页查询字典数据")
    public SaResult getDictDataByDictId(SysDictData sysDictData, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(SysDictData::getDictSort);
        queryWrapper.eq(SysDictData::getDictType, sysDictData.getDictType());
        if (StringUtils.isNotEmpty(sysDictData.getDictLabel())) {
            queryWrapper.like(SysDictData::getDictLabel, sysDictData.getDictLabel());
        }
        if (StringUtils.isNotEmpty(sysDictData.getStatus())) {
            queryWrapper.eq(SysDictData::getStatus, sysDictData.getStatus());
        }
        List<SysDictData> dataByDictId = iSysDictDataService.list(queryWrapper);
        PageInfo<SysDictData> pageInfo = new PageInfo<>(dataByDictId);
        return SaResult.data(pageInfo);
    }

    @PostMapping("/save")
    @ApiOperation("保存字典数据")
    public SaResult save(@RequestBody SysDictData dictData) {
        boolean b = iSysDictDataService.saveOrUpdate(dictData);
        if (b) {
            return SaResult.ok("保存成功");
        } else {
            return SaResult.error("保存失败");
        }
    }

    @PostMapping("/del")
    @ApiOperation("删除字典数据")
    public SaResult del(@RequestBody Map<String, Object> map) {
        if (Objects.nonNull(map.get("dictCode"))) {
            if (iSysDictDataService.removeById((Integer) map.get("dictCode"))) {
                return SaResult.ok("删除成功");
            } else {
                return SaResult.error("删除失败");
            }
        } else if (Objects.nonNull(map.get("dictCodes"))) {
            List<Integer> ids = (List<Integer>) map.get("dictCodes");
            if (iSysDictDataService.removeBatchByIds(ids)) {
                return SaResult.ok("批量删除成功");
            } else {
                return SaResult.error("批量删除失败");
            }
        } else {
            return SaResult.error("删除数据参数出现问题，请稍后再试");
        }
    }

}
